package com.windward.vii.dao.mapper;

import com.windward.vii.dao.entity.AssetUserAccount;
import com.windward.vii.domain.user.SearchParam;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface AssetUserAccountMapper {
    @Delete({
        "delete from asset_user_account",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
        "insert into asset_user_account (id, uid, nickname, ",
        "available_income_coin, available_recharge_coin, ",
        "freeze_coin, minutes, ",
        "connect_times, accept_times, ",
        "weight, newest_weight, ",
        "score, is_recommend_white, ",
        "is_newest_white, price, ",
        "sex, status, info_status, ",
        "online_status, avatar_status, ",
        "create_time, update_time, ",
        "birthday, country, ",
        "province, city, ",
        "avatar, description, ",
        "organization_id)",
        "values (#{id,jdbcType=BIGINT}, #{uid,jdbcType=BIGINT}, #{nickname,jdbcType=VARCHAR}, ",
        "#{availableIncomeCoin,jdbcType=INTEGER}, #{availableRechargeCoin,jdbcType=INTEGER}, ",
        "#{freezeCoin,jdbcType=INTEGER}, #{minutes,jdbcType=INTEGER}, ",
        "#{connectTimes,jdbcType=INTEGER}, #{acceptTimes,jdbcType=INTEGER}, ",
        "#{weight,jdbcType=INTEGER}, #{newestWeight,jdbcType=INTEGER}, ",
        "#{score,jdbcType=INTEGER}, #{isRecommendWhite,jdbcType=BIT}, ",
        "#{isNewestWhite,jdbcType=BIT}, #{price,jdbcType=INTEGER}, ",
        "#{sex,jdbcType=TINYINT}, #{status,jdbcType=TINYINT}, #{infoStatus,jdbcType=TINYINT}, ",
        "#{onlineStatus,jdbcType=TINYINT}, #{avatarStatus,jdbcType=TINYINT}, ",
        "#{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}, ",
        "#{birthday,jdbcType=DATE}, #{country,jdbcType=VARCHAR}, ",
        "#{province,jdbcType=VARCHAR}, #{city,jdbcType=VARCHAR}, ",
        "#{avatar,jdbcType=VARCHAR}, #{description,jdbcType=VARCHAR}, ",
        "#{organizationId,jdbcType=BIGINT})"
    })
    int insert(AssetUserAccount record);

    int insertSelective(AssetUserAccount record);

    @Select({
        "select",
        "id, uid, nickname, available_income_coin, available_recharge_coin, freeze_coin, ",
        "minutes, connect_times, accept_times, weight, newest_weight, score, is_recommend_white, ",
        "is_newest_white, price, sex, status, info_status, online_status, avatar_status, ",
        "create_time, update_time, birthday, country, province, city, avatar, description, ",
        "organization_id",
        "from asset_user_account",
        "where id = #{id,jdbcType=BIGINT}"
    })
    @ResultMap("BaseResultMap")
    AssetUserAccount selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(AssetUserAccount record);

    @Update({
        "update asset_user_account",
        "set uid = #{uid,jdbcType=BIGINT},",
          "nickname = #{nickname,jdbcType=VARCHAR},",
          "available_income_coin = #{availableIncomeCoin,jdbcType=INTEGER},",
          "available_recharge_coin = #{availableRechargeCoin,jdbcType=INTEGER},",
          "freeze_coin = #{freezeCoin,jdbcType=INTEGER},",
          "minutes = #{minutes,jdbcType=INTEGER},",
          "connect_times = #{connectTimes,jdbcType=INTEGER},",
          "accept_times = #{acceptTimes,jdbcType=INTEGER},",
          "weight = #{weight,jdbcType=INTEGER},",
          "newest_weight = #{newestWeight,jdbcType=INTEGER},",
          "score = #{score,jdbcType=INTEGER},",
          "is_recommend_white = #{isRecommendWhite,jdbcType=BIT},",
          "is_newest_white = #{isNewestWhite,jdbcType=BIT},",
          "price = #{price,jdbcType=INTEGER},",
          "sex = #{sex,jdbcType=TINYINT},",
          "status = #{status,jdbcType=TINYINT},",
          "info_status = #{infoStatus,jdbcType=TINYINT},",
          "online_status = #{onlineStatus,jdbcType=TINYINT},",
          "avatar_status = #{avatarStatus,jdbcType=TINYINT},",
          "create_time = #{createTime,jdbcType=TIMESTAMP},",
          "update_time = #{updateTime,jdbcType=TIMESTAMP},",
          "birthday = #{birthday,jdbcType=DATE},",
          "country = #{country,jdbcType=VARCHAR},",
          "province = #{province,jdbcType=VARCHAR},",
          "city = #{city,jdbcType=VARCHAR},",
          "avatar = #{avatar,jdbcType=VARCHAR},",
          "description = #{description,jdbcType=VARCHAR},",
          "organization_id = #{organizationId,jdbcType=BIGINT}",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(AssetUserAccount record);

    /***********以下为自定义方法************************/

    @Select({
            "select",
            "id, uid, nickname, available_income_coin, available_recharge_coin, freeze_coin, ",
            "minutes, connect_times, accept_times, weight, newest_weight, score, is_recommend_white, is_newest_white, price, ",
            "sex, status, info_status, online_status, create_time, update_time, birthday, ",
            "country, province, city, avatar, description, organization_id",
            "from asset_user_account",
            "where uid = #{uid,jdbcType=BIGINT}"
    })
    @ResultMap("BaseResultMap")
    AssetUserAccount selectByUid(Long uid);

    @Select({
            "select",
            "id, uid, nickname, available_income_coin, available_recharge_coin, freeze_coin, ",
            "minutes, connect_times, accept_times, weight, newest_weight, score, is_recommend_white, is_newest_white, price, ",
            "sex, status, info_status, online_status, create_time, update_time, birthday, ",
            "country, province, city, avatar, description, organization_id",
            "from asset_user_account",
            "where uid = #{uid,jdbcType=BIGINT} for update"
    })
    @ResultMap("BaseResultMap")
    AssetUserAccount selectByUidForUpdate(Long uid);

    // 充值：增加"可用充值金额"
    @Update({
            "update asset_user_account",
            "set",
            "available_recharge_coin = available_recharge_coin + #{coin,jdbcType=INTEGER}",
            "where uid = #{uid,jdbcType=BIGINT}"
    })
    int recharge(@Param("uid") Long uid, @Param("coin") Integer coin);

    // 收入：增加"可用收入金额"和"聊天时长"
    @Update({
            "update asset_user_account",
            "set",
            "available_income_coin = available_income_coin + #{coin,jdbcType=INTEGER},",
            "minutes = minutes + #{minutes,jdbcType=INTEGER},",
            "score = score + #{coin,jdbcType=INTEGER} + #{minutes,jdbcType=INTEGER}",
            "where uid = #{uid,jdbcType=BIGINT}"
    })
    int income(@Param("uid") Long uid, @Param("coin") Integer coin, @Param("minutes") Integer minutes);

    // 收入：增加"可用收入金额"，针对礼物和红包，不增加"聊天时长"
    @Update({
            "update asset_user_account",
            "set",
            "available_income_coin = available_income_coin + #{coin,jdbcType=INTEGER},",
            "score = score + #{coin,jdbcType=INTEGER}",
            "where uid = #{uid,jdbcType=BIGINT}"
    })
    int incomeCoin(@Param("uid") Long uid, @Param("coin") Integer coin);

    // 消费：优先消费"可用充值金额"
    @Update({
            "update asset_user_account",
            "set",
            "available_recharge_coin = available_recharge_coin - #{coin,jdbcType=INTEGER}",
            "where uid = #{uid,jdbcType=BIGINT}"
    })
    int consumeRechargeIcon(@Param("uid") Long uid, @Param("coin") Integer coin);

    // 消费：消费"可用收入金额"
    @Update({
            "update asset_user_account",
            "set",
            "available_income_coin = available_income_coin - #{coin,jdbcType=INTEGER}",
            "where uid = #{uid,jdbcType=BIGINT}"
    })
    int consumeIncomeIcon(@Param("uid") Long uid, @Param("coin") Integer coin);

    // 提现冻结：先冻结"可用收入金额"，只有收入金额才能提现
    @Update({
            "update asset_user_account",
            "set",
            "available_income_coin = available_income_coin - #{coin,jdbcType=INTEGER},",
            "freeze_coin = freeze_coin + #{coin,jdbcType=INTEGER}",
            "where uid = #{uid,jdbcType=BIGINT}"
    })
    int freeze(@Param("uid") Long uid, @Param("coin") Integer coin);

    // 提现完成：把"冻结金额减"掉即可
    @Update({
            "update asset_user_account",
            "set",
            "freeze_coin = freeze_coin - #{coin,jdbcType=INTEGER}",
            "where uid = #{uid,jdbcType=BIGINT}"
    })
    int withdraw(@Param("uid") Long uid, @Param("coin") Integer coin);

    // 取消提现："冻结金额"归还给"可用收入金额"即可
    @Update({
            "update asset_user_account",
            "set",
            "available_income_coin = available_income_coin + #{coin,jdbcType=INTEGER},",
            "freeze_coin = freeze_coin - #{coin,jdbcType=INTEGER}",
            "where uid = #{uid,jdbcType=BIGINT}"
    })
    int unfreeze(@Param("uid") Long uid, @Param("coin") Integer coin);


    // 推荐算法：用户连接次数
    @Update({
            "update asset_user_account",
            "set",
            "connect_times = connect_times + 1,",
            "score = score + 1",
            "where uid = #{uid,jdbcType=BIGINT}"
    })
    int updateConnectTimes(Long uid);

    // 推荐算法：主播成功次数
    @Update({
            "update asset_user_account",
            "set",
            "accept_times = accept_times + 1,",
            "score = score + 1",
            "where uid = #{uid,jdbcType=BIGINT}"
    })
    int updateAcceptTimes(Long uid);

    @Select({
            "select uid as id, nickname, minutes, price, sex, status, info_status, online_status, birthday, province, city, avatar, organization_id",
            "from asset_user_account",
            "where is_newest_white = 1 and info_status = 2 and status = 1 order by newest_weight desc, create_time desc, online_status desc"
    })
    @ResultMap("BaseResultMap")
    List<AssetUserAccount> getNewestUserList();

    @Select({
            "select uid as id, nickname, minutes, price, sex, status, info_status, online_status, birthday, province, city, avatar, organization_id",
            "from asset_user_account",
            "where is_recommend_white = 1 and info_status = 2 and status = 1 order by online_status desc, score desc"
    })
    @ResultMap("BaseResultMap")
    List<AssetUserAccount> getRecommendUserList();

    @Update({
            "update asset_user_account",
            "set online_status = #{onlineStatus,jdbcType=TINYINT}",
            "where uid = #{uid,jdbcType=BIGINT}"
    })
    int updateOnlineStatusByUid(@Param("uid") Long uid, @Param("onlineStatus") Integer onlineStatus);

    List<AssetUserAccount> searchUser(SearchParam param);

    List<AssetUserAccount> searchUserByNickname(SearchParam param);

    List<AssetUserAccount> getListByIds(List<Long> uids);

    List<Long> getOnlineUserIds(@Param("sex") Integer sex);

    int updateByUidSelective(AssetUserAccount record);

    @Select({
            "select id, uid, nickname, minutes, price, sex, status, info_status, online_status, birthday, province, city, avatar, organization_id",
            "from asset_user_account"
    })
    @ResultMap("BaseResultMap")
    List<AssetUserAccount> getAllUser();
}